package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.SettleDetailService;
import com.qigao.mall.commons.dto.ProfitDetailResultDto;
import com.qigao.mall.commons.dto.SettleDetailResultDto;
import com.qigao.mall.commons.dto.SettleQueryDto;
import com.qigao.mall.commons.dto.SettleReportResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.UserLevelEnum;
import com.qigao.mall.commons.enums.UserTypeEnum;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.jdbc.mapper.TbSettleDetailMapper;
import com.qigao.mall.jdbc.model.TbProfitDetail;
import com.qigao.mall.jdbc.model.TbSettleDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName market-parent
 * @date 2020-06-2020-06-19 14:55
 * @Description:
 */
@Service
public class SettleDetailServiceImpl implements SettleDetailService {
    @Resource
    private TbSettleDetailMapper tbSettleDetailMapper;
    @Override
    public void insertBatch(List<TbSettleDetail> tbSettleDetails) {
        if(CollectionUtils.isEmpty(tbSettleDetails))return;
        tbSettleDetailMapper.insertBatch(tbSettleDetails);
    }
    @Override
    public void insert(TbSettleDetail tbSettleDetail) {
        tbSettleDetailMapper.insertSelective(tbSettleDetail);
    }

    @Override
    public void saveBatch(List<TbSettleDetail> tbSettleDetails) {
        return;
    }

    @Override
    public Result<PageInfo<SettleDetailResultDto>> listPage(SettleQueryDto settleQueryDto) {
        PageHelper.startPage(settleQueryDto.getPageNum(),settleQueryDto.getPageSize());
        Page<TbSettleDetail> tbSettleDetailPage=(Page<TbSettleDetail>)tbSettleDetailMapper.selectByCond(settleQueryDto);
        return convertToDto(tbSettleDetailPage);
    }

    private Result<PageInfo<SettleDetailResultDto>> convertToDto(Page<TbSettleDetail> tbSettleDetailPage) {
        Page<SettleDetailResultDto> detailResultDtoPage=new Page<SettleDetailResultDto>();
        BeanUtils.copyProperties(tbSettleDetailPage,detailResultDtoPage);
        if(tbSettleDetailPage.getResult()!=null){
            for(TbSettleDetail tbSettleDetail:tbSettleDetailPage.getResult()){
                SettleDetailResultDto settleDetailResultDto=new SettleDetailResultDto();
                BeanUtils.copyProperties(tbSettleDetail,settleDetailResultDto);
                settleDetailResultDto.setSettleDate(DateUtil.getDateFormat(tbSettleDetail.getSettleDate()));
                settleDetailResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbSettleDetail.getCreateTime()));
                settleDetailResultDto.setModiTime(DateUtil.getDateTimeFormat(tbSettleDetail.getModiTime()));
                detailResultDtoPage.add(settleDetailResultDto);
            }
        }
        return Result.newSuccess(new PageInfo<>(detailResultDtoPage));
    }

    @Override
    public List<SettleDetailResultDto> findOrderResultByCond(SettleQueryDto settleQueryDto) {
        List<TbSettleDetail> tbSettleDetailList=tbSettleDetailMapper.selectByCond(settleQueryDto);
        List<SettleDetailResultDto> settleDetailResultDtos=new ArrayList<>();
        for(TbSettleDetail tbSettleDetail:tbSettleDetailList){
            SettleDetailResultDto settleDetailResultDto=new SettleDetailResultDto();
            BeanUtils.copyProperties(tbSettleDetail,settleDetailResultDto);
            settleDetailResultDto.setSettleDate(DateUtil.getDateFormat(tbSettleDetail.getSettleDate()));
            settleDetailResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbSettleDetail.getCreateTime()));
            settleDetailResultDto.setModiTime(DateUtil.getDateTimeFormat(tbSettleDetail.getModiTime()));
            settleDetailResultDtos.add(settleDetailResultDto);
        }
        return settleDetailResultDtos;
    }

    @Override
    public Result<SettleReportResultDto> settleReport(SettleQueryDto settleQueryDto) {
        List<TbSettleDetail> tbSettleDetails=tbSettleDetailMapper.selectByCond(settleQueryDto);
        SettleReportResultDto settleReportResultDto=new SettleReportResultDto();
        settleReportResultDto.setSettleMonth(settleQueryDto.getSettleMonth());
        List<SettleDetailResultDto> settleDetailResultDtos=new ArrayList<>();
        BigDecimal actSettleAmt=BigDecimal.ZERO;
        BigDecimal unsettleAmt=BigDecimal.ZERO;
        if(tbSettleDetails!=null &&tbSettleDetails.size()>=1){
            for(TbSettleDetail tbSettleDetail:tbSettleDetails){
                SettleDetailResultDto settleDetailResultDto=new SettleDetailResultDto();
                BeanUtils.copyProperties(tbSettleDetail,settleDetailResultDto);
                actSettleAmt= actSettleAmt.add(tbSettleDetail.getActSettleAmt());
                unsettleAmt=unsettleAmt.add(tbSettleDetail.getUnsettleAmt());
                settleDetailResultDto.setSettleDate(DateUtil.getDateFormat(tbSettleDetail.getSettleDate()));
                settleDetailResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbSettleDetail.getCreateTime()));
                settleDetailResultDto.setModiTime(DateUtil.getDateTimeFormat(tbSettleDetail.getModiTime()));
                settleDetailResultDtos.add(settleDetailResultDto);
            }
        }
        settleReportResultDto.setActSettleAmt(actSettleAmt);
        settleReportResultDto.setUnsettleAmt(unsettleAmt);
        settleReportResultDto.setSettleDetailResultDtoList(settleDetailResultDtos);
        return Result.newSuccess(settleReportResultDto);
    }
}
